package com.ruoyi.project.sell.order.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sell.order.domain.SellOrder;
import com.ruoyi.project.sell.order.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sell/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderServiceImpl;
    //声明公共资源前缀
    private String prefix = "sell/order";
    //声明公共资源
    @RequiresPermissions("sell:order:view")
    @GetMapping
    public String sellOrder(){
        return prefix+"/order";
    }
    /**
     * 加载订单页面
     */
    @RequiresPermissions("sell:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SellOrder sellOrder){
        startPage();    //分页
        List<SellOrder> orderList = orderServiceImpl.selectSellOrderService(sellOrder);
        System.out.println("加载订单信息:"+orderList);
        //getDataTable(orderList)响应请求分页数据
        return getDataTable(orderList);
    }
    /**
     * 添加订单的单元方法跳转
     */
    @GetMapping("/add")
    public String sellAdd(){
        return prefix+"/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("sell:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SellOrder sellOrder)
    {
        if (UserConstants.Order_NAME_NOT_UNIQUE.equals(orderServiceImpl.checkOrderNameUnique(sellOrder)))
        {
            return error("新增订单'" + sellOrder.getItemName() + "'失败，项目名称已存在");
        }
        return toAjax(orderServiceImpl.insertSellOrder(sellOrder));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkOrderNameUnique")
    @ResponseBody
    public String checkOrderNameUnique(SellOrder sellOrder)
    {
        return orderServiceImpl.checkOrderNameUnique(sellOrder);
    }
    /**
     * 删除订单
     */
    @RequiresPermissions("sell:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(orderServiceImpl.deleteSellOrderByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    /**
     * 修改客户信息，跳转修改页面并完成数据的回显
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        mmap.put("orders", orderServiceImpl.selectOrderById(orderId));
        return prefix + "/edit";
    }
    /**
     * 修改保存公告类别
     */
    @RequiresPermissions("sell:order:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SellOrder sellOrder)
    {
        if (UserConstants.Order_NAME_NOT_UNIQUE.equals(orderServiceImpl.checkOrderNameUnique(sellOrder)))
        {
            return error("修改订单信息'" + sellOrder.getItemName() + "'失败，项目名称已存在");
        }
        return toAjax(orderServiceImpl.updateSellOrderService(sellOrder));
    }

}
